package com.zjh.photoshopstudy.app.user;

import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.Model.CommentEntity;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import static com.zjh.photoshopstudy.Utils.Data.webRoot_URL;

public class UserHttp {

    public static void changePassword(final ChangePasswordActivity activity, long id, final String password) {
        String url = webRoot_URL + "/appUser/edit.html";

        OkGo.<String>get(url)//
                .params("id", id)//
                .params("uPassword", password)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            activity.changeSuccess(password);
                        } else {
                            activity.changeFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public static void uploadIcon(final EditUserActivity editUserActivity, File file) {
        OkGo.<String>post(webRoot_URL + "/appUser/imageUpload")//
                .params("multipartFile", file)//
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        Toast.makeText(editUserActivity, "上传头像中", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        Map<String, Object> map1 = (Map<String, Object>) map.get("data");
                        String url = (String) map1.get("imageUrl");
                        editUserActivity.uploadIconSuccess(url);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });


    }

    public static void updateUser(final EditUserActivity editUserActivity, final AppUserEntity userEntity) {
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd");
        OkGo.<String>post(webRoot_URL + "/appUser/edit.html")//
                .params("id", userEntity.getId())//
                .params("uName", userEntity.getuName())//
                .params("uSex", userEntity.getuSex())//
                .params("uBirthday", bf.format(userEntity.getuBirthday()))//
                .params("uIcon", userEntity.getuIcon())//
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        Toast.makeText(editUserActivity, "上传头像中", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            editUserActivity.updateSuccess(userEntity);
                        } else {
                            editUserActivity.updateFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });


    }

    public static void getComment(final int uId) {
        String url = webRoot_URL + "comment/listByUser.html";
        OkGo.<String>post(url)//
                .params("uId", uId)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            List list = (List) map.get("data");
                            List<CommentEntity> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<CommentEntity>>() {
                            }.getType());
                            for (int i = 0; i < list2.size(); i++) {
                                if (list2.get(i).getAppUserEntity().getId() == uId) {
                                    list2.remove(i);
                                    i--;
                                }
                            }
                            EventBus.getDefault().post(new GetNewComment(list2));
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });

    }

    public static void sendCode(final RetrievePwdActivity retrievePwdActivity, final String uname) {
        OkGo.<String>post(webRoot_URL + "resetPassword.html")//
                .params("uAccountName", uname)//
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        Toast.makeText(retrievePwdActivity, "正在获取验证码。。。", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            String code = (String) map.get("data");
                            retrievePwdActivity.sendCodeSuccess(code, uname);
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });

    }

    public static void resetPassword(final RetrievePwdActivity retrievePwdActivity, final String uname, final String password) {
        String url = webRoot_URL + "/appUser/edit.html";

        OkGo.<String>get(url)//
                .params("uAccountName", uname)//
                .params("uPassword", password)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            retrievePwdActivity.resetSuccess();
                        } else {
                            retrievePwdActivity.resetFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }
}
